function deleteDevice(td,id)
{
  if(confirm("Wollen Sie dieses Gerät wirklich löschen?"))
  {    
   td.innerHTML='<img src="../design/pictures/loading.gif" width="20px" alt="..." />';  
     $.post("ajax/devices.php", {deviceID:id,function:"delete"}).done(function(data){
      var jsonData=$.parseJSON(data);
      if(jsonData.success === "true")
      {
          $(td).closest('tr').fadeTo(400,0,function(){$(this).remove();});
      }
      else
      {
          td.innerHTML='<img src="../design/pictures/error.png" width="20px" alt="..." />';
      }
    }).fail(function(){
          td.innerHTML='<img src="../design/pictures/error.png" width="20px" alt="..." />';
    });
    
  }
}

$(document).ready(function(){
  
  $("#checkAllDevices").change({"class":".deleteCheckbox"},checkAllCheckboxChanged);  
  $('.deleteCheckbox').change({"check":"#checkAllDevices","class":".deleteCheckbox"},classCheckboxChanged);
  
  
  $('#selectInvNumber').change(filterChanged);
  $('#selectName').change(filterChanged);
  $('#searchInput').change(filterChanged);
  $('#delete').click(deleteChecked);

  
  });
function deleteChecked()
{
  if(confirm("Wollen Sie die ausgewählten Geräte wirklich löschen?"))
  {
      $('#load').empty().append('<img src="../design/pictures/loading.gif" width="20px" alt="..." />');
      $.post("ajax/devices.php", ($('#deleteChecked').serialize() + "&" + $.param({function:"deleteChecked"}))).done(function(data){
      var jsonData=$.parseJSON(data);
      if(jsonData.success === "true")
      {
          $.each(jsonData.message, function(i,ID)
          {
              $("#devicesList tbody tr[deviceID='" + ID + "']").fadeTo(400,0,function(){$(this).remove();});
          });
           $('#load').empty();
      }
      else
      {
          $('#load').empty().append('<img src="../design/pictures/error.png" width="20px" alt="..." />');
      }
    }).fail(function(){
        $('#load').empty().append('<img src="../design/pictures/error.png" width="20px" alt="..." />');
    });
  }
}

function classCheckboxChanged(e){
  var allChecked= true;
    $(e.data.class+":visible").each(function(){
     if(!$(this).is(':checked'))
       allChecked = false;
    });
    if(allChecked)
    {
      $(e.data.check).prop('checked',true);
    }
    else
      $(e.data.check).prop('checked',false);
}

function checkAllCheckboxChanged(e){
  if($(this).is(':checked'))
    {
        $(e.data.class+":visible").prop('checked', true);
    }else
    {
        $(e.data.class+":visible").prop('checked', false);
    }
}

function filterChanged(){
  var hiddenTrInRow = 0;
  var searchFieldText =  $('#searchInput').val().trim().toLowerCase();
  var checkInvNumber = $('#selectInvNumber').is(':checked');
  var searchField="";
  if(checkInvNumber)
  {
      searchField = 'td:nth-of-type(6)';
  }else
  {
      searchField = 'td:nth-of-type(4)';
  }
  
  $('#devicesList tbody tr.padding').remove();
  if(searchFieldText != "")
  {
    $('#devicesList tbody tr').show().each(function(){ 
     if($(this).find(searchField).text().trim().toLowerCase().indexOf(searchFieldText) < 0)
      {
        $(this).hide();
        hiddenTrInRow++;
      }
      else
      {
        if((hiddenTrInRow%2)>0)
          $(this).before($('<tr>').attr('class','padding').hide());
        hiddenTrInRow=0;
      }
    });
  }else
  {
    $('#devicesList tbody tr').show();
  }
  $('.deleteCheckbox').trigger('change');
}